package cn.etg.observer.impl;

import cn.etg.observer.Observer;
import cn.etg.observer.Subject;

import java.util.ArrayList;
import java.util.List;

/*
    3D彩票主题 实现 主题接口
 */
public class ObjectFor3D implements Subject {
    // 多个观察者
    private List<Observer> observerList = new ArrayList<>();
    // 通知消息
    String msg;
    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void moveObserver(Observer observer) {
        // 获得观察者位置
        int index = observerList.indexOf(observer);
        if(index>=0){
            observerList.remove(index);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer obs :observerList){
            obs.update(msg);
        }
    }

    public void setMsg(String msg){
        this.msg = msg;
        notifyObserver();
    }
}
